package com.wdy.golden.tinysql.util;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapUtil {
	public static boolean containsAllKeyAndNotNull(Map<String,Object> source, String[] keys) {
		if(source==null) {
			return false;
		}
		
		if(source!=null && (keys!=null && keys.length>0)) {
			boolean flag = true;
			for(String key : keys) {
				if(!source.containsKey(key)) {
					flag = false;
					break;
				}
				if(source.get(key)==null) {
					flag = false;
					break;
				}
			}
			return flag;
		}
		return false;
	}
	
	public static boolean valueEmpty(Map<String,Object> source, String[] keys) {
		if(source==null) {
			return true;
		}
		
		if(source!=null && (keys!=null && keys.length>0)) {
			boolean flag = false;
			for(String key : keys) {
				if(!source.containsKey(key)) {
					flag = true;
					break;
				}
				if(source.get(key)==null) {
					flag = true;
					break;
				}
			}
			return flag;
		}
		return true;
	}

	public static boolean containsAllKeyAndListNotEmpty(Map<String,Object> source, String[] keys) {
		if(source==null) {
			return false;
		}
		
		if(keys!=null && keys.length>0) {
			boolean flag = true;
			for(String key : keys) {
				if(!source.containsKey(key)) {
					flag = false;
					break;
				}
				List<?> keyList = ((List<?>)source.get(key));
				keyList.removeAll(Collections.singleton(null));
				keyList.removeAll(Collections.singleton(""));
				if( keyList==null || keyList.size()<1 ) {
					flag = false;
					break;
				}
			}
			return flag;
		}
		
		return false;
	}

	/**
	 * 
	 * listEmpty:(anyone is null,then return true). <br/> 
	 * 
	 * @author E052535 
	 * @param source
	 * @param keys
	 * @return
	 */
	public static boolean listEmpty(Map<String,Object> source, String[] keys) {
		if(source==null) {
			return true;
		}
		
		if(keys!=null && keys.length>0) {
			boolean flag = false;
			for(String key : keys) {
				if(!source.containsKey(key)) {
					flag = true;
					break;
				}
				List<?> keyList = ((List<?>)source.get(key));
				keyList.removeAll(Collections.singleton(null));
				keyList.removeAll(Collections.singleton(""));
				if( keyList==null || keyList.size()<1 ) {
					flag = true;
					break;
				}
			}
			return flag;
		}
		
		return true;
	}

	public static Map<String,Object> containsStart(Map<String,Object> source, String key) {
		if(source!=null && !source.isEmpty()) {
			Map<String,Object> retMap = new LinkedHashMap<String,Object>();
			for(String sourceKey : source.keySet()){
				if(StringUtil.startsWith(sourceKey, key)) {
					retMap.put(sourceKey, source.get(sourceKey));
				}
			}
			return retMap;
		}
		
		return null;
	}
}
